﻿Public Class FrmSocio
    Private Sub FrmSocio_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        ModoBusqueda()
        Me.SocioTableAdapter.Fill(Me.EjercicioDataSet.Socio)
    End Sub

    Private Sub btnAgregar_Click(sender As System.Object, e As System.EventArgs) Handles btnAgregar.Click
        SocioBindingSource.AddNew()
        ModoEdicion()
    End Sub

    Private Sub btnEliminar_Click(sender As System.Object, e As System.EventArgs) Handles btnEliminar.Click
        If (MessageBox.Show("¿Está seguro que desea eliminar a este socio?", "Eliminar socio", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = Windows.Forms.DialogResult.Yes) Then
            SocioBindingSource.RemoveCurrent()
        End If
    End Sub

    Private Sub btnModificar_Click(sender As System.Object, e As System.EventArgs) Handles btnModificar.Click
        ModoEdicion()
    End Sub

    Private Sub btnAceptar_Click(sender As System.Object, e As System.EventArgs) Handles btnAceptar.Click
        SocioBindingSource.EndEdit()
        SocioDataGridView.Refresh()
        ModoBusqueda()
    End Sub

    Private Sub btnCancelar_Click(sender As System.Object, e As System.EventArgs) Handles btnCancelar.Click
        SocioBindingSource.CancelEdit()
        SocioDataGridView.Refresh()
        ModoBusqueda()
    End Sub

    Private Sub btnCancelarYSalir_Click(sender As System.Object, e As System.EventArgs) Handles btnCancelarYSalir.Click
        Me.Close()
    End Sub

    Private Sub btnGuardar_Click(sender As System.Object, e As System.EventArgs) Handles btnGuardar.Click
        Me.TableAdapterManager.UpdateAll(EjercicioDataSet)
        Me.Close()
    End Sub

    Private Sub ModoEdicion()
        container.Panel1.Enabled = False
        container.Panel2.Enabled = True
    End Sub

    Private Sub ModoBusqueda()
        container.Panel1.Enabled = True
        container.Panel2.Enabled = False
    End Sub

    Private Sub btnBuscar_Click(sender As System.Object, e As System.EventArgs) Handles btnBuscar.Click
        Dim filters As New List(Of String)
        If (Not String.IsNullOrEmpty(txtBusquedaApellido.Text)) Then
            filters.Add("apellido LIKE '%" & txtBusquedaApellido.Text & "%'")
        End If
        If (Not String.IsNullOrEmpty(txtBusquedaNombre.Text)) Then
            filters.Add("nombre LIKE '%" & txtBusquedaNombre.Text & "%'")
        End If
        If (Not String.IsNullOrEmpty(txtBusquedaDNI.Text)) Then
            filters.Add("dni LIKE '%" & txtBusquedaDNI.Text & "%'")
        End If
        If (filters.Count > 0) Then
            SocioBindingSource.Filter = filters(0)
            For i As Integer = 1 To filters.Count - 1
                SocioBindingSource.Filter += " and " & filters(i)
            Next
        Else
            SocioBindingSource.Filter = ""
        End If
    End Sub
End Class